//
//  NetworkTools.swift
//  Sina
//
//  Created by yuanyuan on 15/12/23.
//  Copyright © 2015年 yuanyuan. All rights reserved.
//

import AFNetworking


let dataErrorDomain = "com.baidu.data.error"

//枚举任意类型
enum HTTPMethod: String {
    case GET = "GET"
    case POST = "POST"
}

//网络请求的处理层  以后的网络请求 都使用这个类 来进行数据的获取
class NetworkTools: AFHTTPSessionManager {
    //单例对象
    static let sharedTools: NetworkTools = {
        //设置base url
        let urlString = "https://api.weibo.com/"
        let url = NSURL(string: urlString)
        let tools = NetworkTools(baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    

    //网络请求的核心方法封装

    func requestJsonDict(method: HTTPMethod, urlString: String,parameters:[String : String]?, finished: (dict: [String : AnyObject]?, error: NSError?) -> ()) {
        
        if method == HTTPMethod.POST {
//        发送POST请求
            POST(urlString, parameters: parameters, success: { (_, result) -> Void in
                //请求的成功的回调
                //                    print(result)
                guard let dict = result as? [String : AnyObject] else {
                    //不能够转换为字典数据
                    //执行失败的回调
                    //domain: 反转的域名  com.baidu.error
                    //code : 错误状态码 自定义的错误信息 一般使用 负数
                    let myError = NSError(domain:dataErrorDomain , code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                    print(myError)
                    finished(dict: nil, error: myError)
                    return
                }
                
                //执行成功的回调
                finished(dict: dict, error: nil)
                
                
                
                }, failure: { (_, error) -> Void in
                    //执行失败的回调
                    finished(dict: nil, error: error)
                    //请求失败的回调
                    print(error)
                    
                    
            })
        
        
        }else {
        
        GET(urlString, parameters: parameters, success: { (_, result) -> Void in
            //请求的成功的回调
            //                    print(result)
            guard let dict = result as? [String : AnyObject] else {
                //不能够转换为字典数据
                //执行失败的回调
                //domain: 反转的域名  com.baidu.error
                //code : 错误状态码 自定义的错误信息 一般使用 负数
                let myError = NSError(domain:dataErrorDomain , code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                print(myError)
                finished(dict: nil, error: myError)
                return
            }
            
            //执行成功的回调
            finished(dict: dict, error: nil)
            
            
            }, failure: { (_, error) -> Void in
                
                //执行失败的回调
                finished(dict: nil, error: error)
                //请求失败的回调
                print(error)
                
        })
        
        
        
        }
        
}
}
